package other.xsy.passlys.receivable;

import com.rkhd.platform.sdk.data.model.InvoiceDetail__c;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 删除开票明细，重新计算开票相关金额字段
 * @Author 黎炎松
 * 2024-08-08 18:38
 */
public class ClaimDel implements Trigger {
    private static final Logger logger = LoggerFactory.getLogger();

    public static void main(String[] args) {

    }

    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        logger.debug("删除认领主表 触发开始 ");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<XObject> objList = new ArrayList<>();
        for (XObject xObject : dataList) {
            objList.add(xObject);

            dataResultList.add(new DataResult(true, "", xObject));
        }

        try {
            // 获取明细下的 booking
            Object bookingStr = objList.get(0).getAttribute("bookingStr__c");
            logger.debug("删除认领主表 触发开始 ==> 开始处理: " + bookingStr);
            if (bookingStr == null) {
                return new TriggerResponse(true, "处理完成", dataResultList);
            }

            // 获取booking 和 银行流水
            String[] split = bookingStr.toString().split("\\|");
            if (split.length < 2) {
                return new TriggerResponse(true, "处理完成", dataResultList);
            }

            // 获取bookingId
            String bookingIdsStr = split[0];
            logger.debug("删除认领主表 触发开始 ==> bookingIdsStr: " + bookingIdsStr);
            List<Long> bookingIdList = Arrays.stream(bookingIdsStr.split(",")).map(v -> Long.valueOf(v)).distinct().collect(Collectors.toList());


            // 获取银行流水 bankStatementIds
            String bankStatementIdsStr = split[1];
            logger.debug("删除认领主表 触发开始 ==> bankStatementIdsStr: " + bankStatementIdsStr);
            List<Long> bankStatementIdList = Arrays.stream(bankStatementIdsStr.split(",")).map(v -> Long.valueOf(v)).distinct().collect(Collectors.toList());

            // 触发修改相关认领字段
            logger.debug("删除认领主表 触发开始 bookingIdList " + bookingIdList);
            logger.debug("删除认领主表 触发开始 bankStatementIdList " + bankStatementIdList);
            ReceivableService receivableService = new ReceivableService();
            receivableService.calcClaimAmounByBooking(bookingIdList);
            receivableService.upBankStatementAmount(bankStatementIdList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        logger.debug("删除认领主表 触发开始 ==> 处理结束");
        return new TriggerResponse(true, "处理完成", dataResultList);
    }

}
